package cn.wolfcode.wolf2w.util;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

public abstract class PagingUtil {

    //<T>占位符
    public static <T> Page<T> query(Query query,MongoTemplate template,Class<T> clz,Pageable pageable) {


        //获取count,查询数据总条数,注意不需要加入分页条件
        long count = template.count(query, clz);
        //判断count
        if (count == 0){
            return Page.empty();
        }

        //封装条件
        query.with(pageable);

        //获取list, 查询当前页的数据,需要分页条件
        List<T> list = template.find(query, clz);

        //返回分页数据
        return new PageImpl(list, pageable, count);
    }
}
